package com.example.administrator.funchat.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.funchat.R;
import com.example.administrator.funchat.model.User;

import org.xutils.x;

import java.util.List;

/**
 * 通讯录适配器
 * Created by Administrator on 2016/6/24.
 */
public class ContactAdapter extends BaseAdapter {

    private Context context;
    private List<User> userData;
    private LayoutInflater inflater;

    public ContactAdapter(Context context, List<User> userData) {
        this.context = context;
        this.userData = userData;
        inflater = LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        return userData.size();
    }

    @Override
    public Object getItem(int position) {
        return userData.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ViewHolder viewHolder;
        if (convertView == null) {
            view = inflater.inflate(R.layout.contact_item, null);
            viewHolder = new ViewHolder();
            viewHolder.mailImage = (ImageView) view.findViewById(R.id.mail_image);
            viewHolder.mailFrom = (TextView) view.findViewById(R.id.mail_from);
            viewHolder.tv_number_message = (TextView) view.findViewById(R.id.tv_number_message);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        x.image().bind(viewHolder.mailImage, userData.get(position).getHeadImg());
        viewHolder.mailFrom.setText(userData.get(position).getName());
        if (userData.get(position).getNum() > 0) {
            viewHolder.tv_number_message.setVisibility(View.VISIBLE);
            viewHolder.tv_number_message.setText(userData.get(position).getNum() + "");
        }
        return view;

    }

    class ViewHolder {
        ImageView mailImage;
        TextView mailFrom;
        TextView tv_number_message;//消息数量

    }
}


